natural deduction metalanguage, practical foundations
type theory (dependent, intensional, observational type theory, homotopy type theory)
computational trinitarianism =
propositions as types +programs as proofs +relation type theory/category theory
The store comonad (also costate comonad, as it is left adjoint to the state monad) is a co-monad (in computer science) used to implement computational storage and retrieval of data (databases) in functional programming.
Concretely, the coalgebras over the store monad are equivalently the well-behaved lens-data structures (O’Connor (2010), (2011); see there) used to inspect and edit fields (“views”) in databases.
By default, the costate comonad is understood to be defined on a cartesian closed category whose internal hom we denote by .
This means that given an object there is a pair of adjoint functors
Notice that
the unit of this adjunction (being the adjunct of the identity morphism on ) is:
the counit of this adjunction (being the adjunct of the identity morphism on ) is the evaluation map
While the -state monad is the monad induced by this adjunction (1)
the -store comonad is the induced comonad
whose counit is (3) and whose duplicate operation (comonad coproduct) is given by the unit (2) as
Therefore the comonadic extend operation sends a coKleisli morphism
to
In applications of comonads in computer science one thinks of
as an address type,
a data base of -indexed -data
hence of the -comonad on as providing the computational context consisting of data base (of “stored -values”, whence the name store comonad) and an address.
Thus the obtain-operation (3) literally obtains (reads, extracts) the memory content at the given address.
More generally, for a closed monoidal category (not necessarily cartesian), every object still still gives an internal-hom adjunction of the form (1)
and the induced monad and comonad may be understood as substructural forms of the state monad and store comonad, respectively.
Particularly when is the category of complex vector spaces (“-linear spaces”) this gives a linear form of the costate comonad, discussed at
In a locally Cartesian closed category/dependent type theory , then to every type is associated its base change adjoint triple
In terms of this the store comonad is the composite
of context extension, followed by dependent product , followed by context extension, followed by dependent sum.
Here is called the function monad or reader monad and is the writer comonad.
We spell out the data of comodules over the classical Store comonad defined above.
(Beware that we are switching from using “” for the state space to using it for the argument of the comonad – this in order to be closer to the notation used at lens in computer science.)
Unwinding the definitions, a -store comodule structure on a type is a map of the form
and hence consists, equivalently, of a pair of functions of the form
On this data, the counitality condition of a comodule (5) requires that the following composite is the identity
hence that
while the coaction property requires that the following diagram commutes
which amounts to the two conditions
and
This data — a pair of maps (6) satisfying the get-put law (7), the put-get law (8) and the put-put law (9) – is known as a lawful lens in computer science (see there).
Hence, as first observed by O’Connor 2010:
Classical -store comodules are equivalently lawful lenses with “view” .
function monad (reader monad)
Bartosz Milewski (compiled by Igal Tabachnik), §23.6 in: Category Theory for Programmers, Blurb (2019) [pdf, github, webpage, ISBN:9780464243878]
Tarmo Uustalu, slide 14 of: Monads and Interaction Lecture 3 , lecture notes for MGS 2021 (2021) [pdf, pdf]
The observation that lenses (in computer science) are equivalently the coalgebras of the costate comonad (cf. monads in computer science) is due to:
Russell O’Connor, Lenses Are Exactly the Coalgebras for the Store Comonad (30th Nov 2010) [r6research:23705]
Russell O'Connor, Functor is to Lens as Applicative is to Biplate: Introducing Multiplate, contribution to ICFP ‘11: ACM SIGPLAN International Conference on Functional Programming (2011) [arXiv:1103.2841]
Early review:
Further details:
Further review:
Bartosz Milewski, pp 240 in: The Dao of Functional Programming (2023) [pdf, github]
Last revised on January 24, 2024 at 03:14:22. See the history of this page for a list of all contributions to it.